
capture program drop sumstat
program define sumstat 
	syntax varlist, name(string) replace																															///Basic Specs
	[flush_l(varlist)] [flush_r(varlist)] [flush_c(varlist)] [format_0(varlist)] [format_1(varlist)] [format_2(varlist)] [format_3(varlist)] [format_lab(varlist)] 	///Variable Format Specs
	headlabel(string) [grouplabel(string)] [head2label(string)]																										///Labels
	first(string) long(string) landscape(string) width(string) grouping(string) [grouporder(string)] [head2(numlist)]												///Table Format Specs	 
	
tempname file
file open `file' using "`name'.tex", write `replace'

preserve

/*Locals*/
local flush l r c
local size: word count `varlist'
local group_no: word count `grouporder'
local head2_no: word count `head2'
local other: list varlist-first	
local rows=(_N+`group_no')

/*Generate Ordering Variables*/
gen order_bw=.
forval n = 1/`group_no'{
	local b: word `n' of `grouporder'
	replace order_bw=`n' if group=="`b'" 
}
gen order_wi=_n

/*Convert Vars in Specified Format*/
foreach x in `format_lab' {       				
	sdecode `x', replace
}
local dec 0 1 2 3
foreach d of local dec{
	foreach x in `format_`d''{
		gen s_`x' = string(`x', "%9.`d'fc")
		drop `x'
		rename s_`x' `x'
		replace `x'="--" if `x'=="."
	}
}

/*Assign Labels*/
forval n = 1/`size'{
    local a: word `n' of `headlabel'
    local b: word `n' of `varlist'
    label var `b' "`a'"
}

forval n = 1/`group_no'{
    local a: word `n' of `grouplabel'
    local b: word `n' of `grouporder'
	replace group="`a'" if group=="`b'" 
}

/*Implement Panel Structure*/
if "`grouping'"!="no"{
	bysort group order_bw: ingap, row(`first') gap(head_id)
	replace order_wi=0 if head_id==1
	replace `first' = "\cmidrule(lr){1-`size'}\multicolumn{`size'}{l}{\thead[l]{\textbf{"+group+"}}}" if head_id == 1
	replace `first' = "\multicolumn{`size'}{l}{\thead[l]{\textbf{"+group+"}}}" if order_bw==1 & order_wi==0
	bysort order_bw (order_wi): replace `first' = /*"\cmidrule(lr){1-`size'}"+*/"{"+`first'[_n]+"}" if _n == 2 
}

if "`grouping'"=="no"{
	gen head_id=0
}


/*Table Header*/
if "`landscape'"=="yes"{
	file write `file' "\begin{landscape}"_n
}

if "`long'"=="no"{
	file write `file' "\begin{tabular*}{`width'\textwidth}{@{\hskip\tabcolsep\extracolsep\fill}"
	foreach x in `varlist'{
		foreach f of local flush{
			local `f': list x & flush_`f'
			foreach y of local `f'{
				file write `file' "`f'" 
			}
		}
	}
	file write `file' "}"_n
	file write `file' "\toprule"_n
	
	
	if !missing("`head2'"){
		file write `file' "\multicolumn{"`"`:word 1 of `head2''"'"}{c}{\thead{"`"`:word 1 of `head2label''"'"}}" 
		forval n = 2/`head2_no'{
			file write `file' "&\multicolumn{"`"`:word `n' of `head2''"'"}{c}{\thead{"`"`:word `n' of `head2label''"'"}}" _n
		}
		file write `file' "\\"_n
		
		local floor=1
		forval n = 2/`head2_no'{
			local c_l=`floor'+1
			local c_u=`c_l'+`:word `n' of `head2''-1
			local floor=`c_u'
			file write `file' "\cmidrule(lr){`c_l'-`c_u'}" _n
		}
	}
	
	file write `file' " \thead{"`"`: var label `first''"'"} "	
	foreach x in `other' {
		file write `file' " &  \thead{"`"`: var label `x''"'"} " 
	} 
	file write `file' "\\"_n
	file write `file' "\midrule"_n
}

/*Table Body*/
forvalues i = 1/`rows'{
	if "`grouping'"=="yes"{

		if head_id[`i']==1{
			foreach x in `first'{
				local val = `x'[`i']
				file write `file' "`val'"				
			}
			foreach x in `other'{
				file write `file' "~"				
			}
		}
		else{
			foreach x in `first'{
				local val = `x'[`i']
				file write `file' "\hspace{+1em}\makecell[l]{\footnotesize `val'}"
			}	
			foreach x in `other'{
				local val = `x'[`i']
				file write `file' "& \makecell[c]{\footnotesize `val'} "
			}
		}
	}
	
	if "`grouping'"=="no"{

		if head_id[`i']==1{
			foreach x in `first'{
				local val = `x'[`i']
				file write `file' "`val'"				
			}
			foreach x in `other'{
				file write `file' "~"				
			}
		}
		else{
			foreach x in `first'{
				local val = `x'[`i']
				file write `file' "\makecell[l]{\footnotesize `val'}"
			}	
			foreach x in `other'{
				local val = `x'[`i']
				file write `file' "& \makecell[c]{\footnotesize `val'} "
			}
		}
	}
	file write `file' "\\"_n
}

/*Table Footer*/
if "`long'"=="no"{
	file write `file' "\bottomrule"_n
	file write `file' "\end{tabular*}"_n
}

if "`landscape'"=="yes"{
	file write `file' "\begin{landscape}"_n
}

file close `file'

restore

end

